//
//  UrbanNewsViewController.m
//  UrbanManagement
//
//  Created by Can on 18/4/12.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "UrbanNewsViewController.h"
#import "UrbanNewsTableViewCell.h"
#import "UrbanDetailsViewController.h"

@interface UrbanNewsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *newsList;
@property (nonatomic, assign) NSInteger pageNumber;

@end

@implementation UrbanNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTitleWithTitle:@"消息"];
    
    _pageNumber = 1;
    
    _newsList = [NSMutableArray new];
    
    [self creatView];
    
    [self getRecordList];
}

- (void)creatView{
    CGFloat height = NavBarHeight;
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH - height) style:UITableViewStylePlain];
    _tableView.backgroundColor = Color(247, 247, 247);
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    _tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    
}

- (void)refresh
{
 
    [_tableView.mj_header endRefreshing];
}

//上拉加载更多
-(void)footerRefresh
{
    
}

#pragma mark - tabledelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _newsList.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60*screenW_6;//section头部高度
}

//headView 设置
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 60*screenW_6)];
    
    CGFloat width = [TomamaUI getStringWeithtWith:[_newsList[section] objectForKey:@"updateDate"] andHeigth:13*screenW_6 andFont:[UIFont systemFontOfSize:13*screenW_6]];

    UILabel *timeLable = [TomamaUI createLabel:CGRectMake(0, 0, width + 26*screenW_6, 30*screenW_6) textAligment:1 font:[UIFont systemFontOfSize:13*screenW_6] textColor:Color(153, 153, 153) text:[_newsList[section] objectForKey:@"updateDate"]];
    timeLable.backgroundColor = Color(234, 233, 233);
    //设置圆角
    timeLable.layer.cornerRadius = 10;
    //将多余的部分切掉
    timeLable.layer.masksToBounds = YES;
    [view addSubview:timeLable];
    timeLable.center = CGPointMake(view.center.x, view.center.y);
    
    return view;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID = @"NewsCell";
    UrbanNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UrbanNewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    [cell loadData:_newsList[indexPath.section]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [UrbanNewsTableViewCell cellHeight:_newsList[indexPath.section]];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UrbanDetailsViewController *detailVc = [UrbanDetailsViewController new];
    detailVc.ID = [_newsList[indexPath.section] objectForKey:@"id"];
    [self.navigationController pushViewController:detailVc animated:YES];
}

- (void)getRecordList
{
    [HudHelper showHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSMutableDictionary *dic = [NSMutableDictionary new];
        [dic setValue:@"3" forKey:@"newsType"];
        [dic setValue:[NSString stringWithFormat:@"%ld",(long)self->_pageNumber] forKey:@"pageNo"];
        [dic setValue:@"10" forKey:@"pageSize"];
        
        [HttpRequest PostrequestUrl:@"jeesite/appInterface/news/newsList" Dic:dic completion:^(NSDictionary *responseDic){
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                NSLog(@"list:%@",responseDic);
                if ([responseDic[@"success"] boolValue]) {//请求成功
                    [self->_newsList addObjectsFromArray:[[responseDic objectForKey:@"result"] objectForKey:@"list"]];
                    
                    if([[[responseDic objectForKey:@"result"] objectForKey:@"count"] floatValue]/10 <= self->_pageNumber){
                        self.tableView.mj_footer.hidden = YES;
                    }else{
                        self.tableView.mj_footer.hidden = NO;
                    }
                    
                    [self->_tableView reloadData];
                }
                
                if ([self->_tableView.mj_header isRefreshing]) {
                    [self->_tableView.mj_header endRefreshing];
                }
                if ([self->_tableView.mj_footer isRefreshing]) {
                    
                    [self->_tableView.mj_footer endRefreshing];
                }
            });
        }];
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
